<template>
  <page-main class="page-login">
    <div class="logo">
        <van-image :src="pic.logo" width="178px" />
        <p>广医云</p>
    </div>
    <div class="login-btn">
        <van-button type="primary" block round @click="wechatLogin" icon="wechat">微信一键登录</van-button>
        <!-- <p @click="accountLogin">账号密码登录</p> -->
    </div>
    <van-submit-bar>
        <div class="agreement">
            <van-checkbox icon-size="16" checked-color="#1989fa" v-model="checked">已阅读并同意<span @click.stop="$refs.showAgreement.init('user')">《用户协议》</span></van-checkbox>
        </div>
    </van-submit-bar>
    <show-agreement ref="showAgreement" />
</page-main>
</template>

<script>
import showAgreement from '@/components/ShowAgreement';
import {appid} from '@/assets/js/config';
export default {
    data(){
        return{
            pic:{
                logo: '/'
            },
            checked: true,
            code: null,
        }
    },
    components:{
        showAgreement,
    },
    created(){
        this.getCode();
    },
    methods:{
        handleLogin(type){
            this.$refs[type].init();
        },
        wechatLogin(){  
            // this.$store.dispatch('wechatLogin', this.code); 
            this.$http('post', '/auth/oauth/token?mobile=WX@'+this.code+'&grant_type=mobile&code=xxx', {mobile: 'WX', grant_type: 'mobile',code: this.code}, (data,msg) => {
                console.log(data);
                localStorage.setItem('token', data.data.access_token);
                this.$toast.success('登录成功！');
                this.$store.dispatch('getCurrentPatient');
                this.$util.gotoPage('/layout/home');
            },msg => {
                this.$dialog.alert({
                    title: '提示',
                    message: msg,
                }).then(() => {
                    
                });                
            }, {}, 'all');
        },
        accountLogin(){
            this.$util.gotoPage('/login/account', {code: this.code});
        },
        //拿到微信code
        getCode(){
            let code = this.GetParam(window.location.href.split("#")[0], "code");
            if (!code) {
                let url = encodeURIComponent('https://h5.hospital.caochuanxi.com#/login/index');
                window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+url+'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'
            } else {
                this.code = this.GetParam(window.location.href, "code");
            }
        },
        // 截取code
        GetParam(url, code) {
            url = url + "";
            let regstr = "/(\\?|\\&)" + code + "=([^\\&]+)/";
            let reg = eval(regstr);
            //eval可以将 regstr字符串转换为 正则表达式
            let result = url.match(reg);
            if (result && result[2]) {
                return result[2];
            }
        },      
    }
}
</script>

<style>

</style>